package org.mspring.mlog.service;

import org.apache.commons.lang3.StringUtils;
import org.mspring.mlog.cache.ConfigCacheService;
import org.mspring.mlog.dao.ConfigEntityDao;
import org.mspring.mlog.entity.ConfigEntity;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.db.dao.sql.builder.part.condition.SqlCondition;
import org.mspring.nbee.db.dao.sql.pager.SqlPager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gao Youbo
 * @since 2015-01-03 18:08:20
 */
@Service
public class ConfigEntityService {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigEntityService.class);

    private ConfigEntityDao configEntityDao;
    private ConfigCacheService configCacheService;

    public String getConfigValue(String groupName, String name) {
        ConfigEntity config = findByName(groupName, name);
        if (config == null) {
            return StringUtils.EMPTY;
        }
        return config.getValue();
    }

    /**
     * 根据分组和名称查找
     *
     * @param groupName
     * @param name
     * @return
     */
    public ConfigEntity findByName(String groupName, String name) {
        ConfigEntity config = configCacheService.getConfig(groupName, name);
        if (config == null) {
            config = configEntityDao.findByName(groupName, name);
            configCacheService.setConfig(groupName, name, config);
        }
        return config;
    }

    /**
     * 加载所有
     *
     * @return
     */
    public List<ConfigEntity> listAll() {
        return configEntityDao.listAll();
    }

    public PageResult<ConfigEntity> list(List<SqlCondition> conditions, SqlPager pager) {
        return configEntityDao.listPage(conditions, pager);
    }


    @Transactional
    public void insertOrUpdateConfigs(List<ConfigEntity> configs) {
        for (ConfigEntity config : configs) {
            ConfigEntity temp = findByName(config.getGroupName(), config.getName());
            if (temp != null) {
                config.setId(temp.getId());
            }
            boolean flag = insertOrUpdate(config);
            if (flag) {
                configCacheService.removeConfig(config.getGroupName(), config.getName());
            }
        }
    }

    @Transactional
    public boolean insertOrUpdate(ConfigEntity config) {
        try {
            if (config.getId() == null || config.getId() <= 0) {
                return configEntityDao.insert(config);
            } else {
                return configEntityDao.update(config);
            }
        } finally {
            configCacheService.removeConfig(config.getGroupName(), config.getName());
        }
    }

    @Transactional
    public boolean insert(ConfigEntity config) {
        return configEntityDao.insert(config);
    }

    @Transactional
    public boolean update(ConfigEntity config) {
        boolean flag = configEntityDao.update(config);
        if (flag) {
            configCacheService.removeConfig(config.getGroupName(), config.getName());
        }
        return flag;
    }

    @Autowired
    public void setConfigEntityDao(ConfigEntityDao configEntityDao) {
        this.configEntityDao = configEntityDao;
    }

    @Autowired
    public void setConfigCacheService(ConfigCacheService configCacheService) {
        this.configCacheService = configCacheService;
    }
}